{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 以下是densenet实现过程描述\n",
    "对growth和稠密连接的理解：\n",
    "    densenet的主要创新点是各层feature之间的稠密连接，这样做可以有效的减少梯度消失现象，因为在反向传播过程中可以把梯度直接传递给网络开始的几层，同时把前一层与后一层连接，也实现了特征的重复利用。\n",
    "    同时，通过设置恰当的growth数，把网络变得很窄，让每一个layer只学习非常少的特征，达到了去除冗余的目的，大幅减少了训练的参数，加快了训练的速度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "            #第一个卷积层，卷集核大小为7*7,步长stride为2，通道数为2*growth=48，\n",
    "            net = slim.conv2d(images, 48, [7, 7],stride=2, scope=scope + '_pre_conv')\n",
    "            end_points[scope + '_pre_conv'] = net\n",
    "            #最大池化层，卷集核大小为3*3,步长stride为2，图片尺寸变为原来的一半\n",
    "            net = slim.max_pool2d(net, [3, 3],stride=2, scope=scope + '_pre_pool')\n",
    "            end_points[scope + '_pre_pool'] = net\n",
    "            #第一个dense block ，在一个block内图片的维度不改变，只是增加了channel数，一个block包含6个layer，每个layer              中1*1和3*3conv层\n",
    "            #这里的conv层指的是bn-relu-conve2d-dropout四个layer\n",
    "            net = block(net, 6, growth, scope= scope + '_denseblock1')\n",
    "            end_points[scope + '_denseblock1'] = net\n",
    "            #transitionlayer,目的是使用1*1卷积进行降维，减小通道数，在通过池化层把图像尺寸减小为原来的一半\n",
    "            net = bn_act_conv_drp(net, reduce_dim(net), [1,1], scope='transition-conv1')\n",
    "            net = slim.avg_pool2d(net, [2, 2], stride=2, scope='transition-pool1')\n",
    "            end_points['transition-pool1'] = net\n",
    "            #第2个dense block\n",
    "            net = block(net, 12, growth, scope= scope + '_denseblock2')\n",
    "            end_points[scope + '_denseblock2'] = net\n",
    "            #第2个transitionlayer\n",
    "            net = bn_act_conv_drp(net, reduce_dim(net), [1,1], scope='transition-conv2')\n",
    "            net = slim.avg_pool2d(net, [2, 2], stride=2, scope='transition-pool2')\n",
    "            end_points['transition-pool2'] = net\n",
    "            #第3个dense block\n",
    "            net = block(net, 24, growth, scope= scope + '_denseblock3')\n",
    "            end_points[scope + '_denseblock3'] = net\n",
    "            #第3个transitionlayer\n",
    "            net = bn_act_conv_drp(net, reduce_dim(net), [1,1], scope='transition-conv3')\n",
    "            net = slim.avg_pool2d(net, [2, 2], stride=2, scope='transition-pool3')\n",
    "            end_points['transition-pool3'] = net\n",
    "            #第4个dense block\n",
    "            net = block(net, 16, growth, scope= scope + '_denseblock4')\n",
    "            end_points[scope + '_denseblock4'] = net\n",
    "            #全局平均池化，对每一个feature作avg_pooling,得到平均值  \n",
    "            net = slim.avg_pool2d(net, int(net.shape[1]), stride=1, scope='glb-pool')\n",
    "            net = slim.flatten(net)\n",
    "            end_points['glb-pool'] = net\n",
    "            #全连接层\n",
    "            logits = slim.fully_connected(net, num_classes, biases_initializer=tf.zeros_initializer(),\n",
    "                                  weights_initializer=trunc_normal(1/192.0), activation_fn=None, scope='logits')\n",
    "            end_points['Predictions'] = tf.nn.softmax(logits, name='Predictions')\n",
    "            ##########################\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
